package org.jef.codegen.template;

import pers.cz.codegen.codegen.CodeGenerator;
import pers.cz.codegen.dbtools.MetaOperator;
import pers.cz.codegen.dbtools.TableInfo;
import pers.cz.codegen.describe.JavaUnit;
import pers.cz.codegen.template.CommentTemplate;
import pers.cz.jefconfig.tools.StringUtils;

import java.io.File;
import java.nio.charset.Charset;
import java.sql.SQLException;

/*
 * @version 1.0
 * @description: mybatis的mapper接口自动生成
 * @author: ChengZhi 0336
 * @create: 2024-08-07 10:35
 */

public class MybatisMapperCodeGenerator  extends CodeGenerator {

    private String tableName = "%";

    public MybatisMapperCodeGenerator(String tableName) {
        this.tableName = tableName;
    }

    public MybatisMapperCodeGenerator() {

    }

    /*
     * 自动生成实体代码
     * @throws SQLException
     */
    @Override
    public void generateCode() throws SQLException {

        MetaOperator metaOperator = new MetaOperator(tableName);
        for (TableInfo tableInfo : metaOperator.getAllTables()) {
            String tableClassName = StringUtils.getCamelCase(tableInfo.getTableName().toLowerCase());
            JavaUnit javaUnit = new JavaUnit(tableClassName + "Mapper", JavaUnit.UnitType.INTERFACE);
            javaUnit.setIpackage(getIpackage());
            javaUnit.addImport("com.baomidou.mybatisplus.core.mapper.BaseMapper");
            javaUnit.addImport("org.apache.ibatis.annotations.Mapper");
            javaUnit.addImport("org.jef.upms.api.entity." + tableClassName);
            javaUnit.setComment(CommentTemplate.classComment);
            javaUnit.setDescribe(tableInfo.getComment());
            javaUnit.addAnnotation("@Mapper");

            javaUnit.setParent("BaseMapper<" + tableClassName + ">");
            File testFolder = new File(getFileFolder());
            javaUnit.saveToSrcFolder(testFolder, Charset.forName("UTF8"));
        }
    }

    @Override
    public void enhanceCode() throws Exception {

    }
}
